package com.newtouch.bxzs.business.enclosure.controller;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.apache.commons.io.IOUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.InputStreamResource;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.newtouch.bxzs.business.enclosure.model.FileVO;
import com.newtouch.bxzs.business.enclosure.service.AttachmentService;
import com.newtouch.bxzs.common.base.model.vo.EnclosureVO;
import com.newtouch.bxzs.common.base.response.BaseResponse;
import com.newtouch.bxzs.common.base.response.RespGenerator;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

/**
 * 
 * 附件操作controller类
 *
 * <p>
 * <b>History:</b>
 * <table border="1">
 * <tr>
 * <th>Date</th>
 * <th>Operator</th>
 * <th>Memo</th>
 * </tr>
 * <tr>
 * <td>2021年1月4日</td>
 * <td>Amour</td>
 * <td>Create</td>
 * </tr>
 * </table>
 * 
 * @author Amour
 * 
 * @version 1.0.0
 * @since 1.0.0
 */
@Api(tags = "文件服务")
@RestController
@RequestMapping(value = "/enclosure")
@SuppressWarnings("unchecked")
public class EnclosureController {

	/** 静态变量：系统日志 */
	private static final Log logger = LogFactory.getLog(EnclosureController.class);

	@Autowired
	private AttachmentService attachmentService;

	@Value("${file.basePath}")
	private String fileBasePath;

	@PostMapping(value = "/upload")
	@ApiOperation(value = "单个文件上传")
	public BaseResponse<String> upload(
			@RequestParam("file") @ApiParam(name = "file", value = "文件,form表单类型", required = true) MultipartFile file,
			@RequestParam("url") @ApiParam(name = "url", value = "保存路径,一般使用本次操作的数据主键ID", required = true) String url,
			@RequestParam("type") @ApiParam(name = "type", value = "附件类型,通过枚举获取", required = true) String type) {
		logger.info("文件上传");
		return RespGenerator.returnOK(attachmentService.uploadFile(file, url, type));
	}

	@PostMapping(value = "/uploads")
	@ApiOperation(value = "批量上传附件asa")
	public BaseResponse<List<String>> upload(
			@RequestParam("files") @ApiParam(name = "files", value = "需要新增的文件集合数组,form表单类型", required = true) List<MultipartFile> files,
			@RequestParam(value = "fileNames", required = false) @ApiParam(name = "fileNames", value = "本次更新时不进行操作的文件名集合数组") List<String> targetFileNames,
			@RequestParam("url") @ApiParam(name = "url", value = "保存路径,一般使用本次操作的数据主键ID", required = true) String url,
			@RequestParam("type") @ApiParam(name = "type", value = "附件类型,通过枚举获取", required = true) String type) {
		logger.info("文件上传");
		return RespGenerator.returnOK(attachmentService.uploadFile(files, targetFileNames, url, type));
	}

	@PostMapping(value = "/getEnclosureList")
	@ApiOperation(value = "查询附件信息")
	public BaseResponse<HashMap<String, List<String>>> getEnclosureList(@RequestBody EnclosureVO enclosureVO) {
		return RespGenerator.returnOK(attachmentService.getEnclosureList(enclosureVO));
	}

	@PostMapping(value = "/download")
	@ApiOperation(value = "下载附件")
	public ResponseEntity<InputStreamResource> downloadFile(
			@RequestParam("url") @ApiParam(name = "url", value = "附件完整路径", required = true) String url)
			throws IOException {
		return attachmentService.downloadFile(url);
	}

	@ApiOperation(value = "文件预览，仅支持pdf格式")
	@PostMapping("/readPdf")
	public void readPdf(HttpServletResponse response, @RequestBody FileVO fileVO) throws IOException {
		String filePath = attachmentService.createFileUrl(fileVO);
		response.reset();
		response.setContentType("application/pdf");
		OutputStream outputStream = response.getOutputStream();
		try {
			File file = new File(filePath);
			FileInputStream fileInputStream = new FileInputStream(file);
			IOUtils.write(IOUtils.toByteArray(fileInputStream), outputStream);
			response.setHeader("Content-Disposition", "inline; filename= file");
			outputStream.flush();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			outputStream.close();
		}
	}
}
